package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }

    /*@Override
    public List<Clazz> selectAll(Clazz clazz, Integer page, Integer size) {
        return clazzMapper.queryClazzs(clazz,page,size);
    }*/

    @Override
    public void delete(Integer id) {
        //1.判断该班级下是否有学生
        Integer count = studentMapper.countByClazzId(id);

        //2.如果有，提示错误信息
        if (count > 0){
            //抛异常
            throw new BusinessException("对不起，该班级下有学生，不能直接删除");
        }
        //3.没有，在删除
        clazzMapper.deleteById(id);//重新定义的一个方法根据id删除部门
    }

    @Override
    public Clazz getById(Integer id) {
         return clazzMapper.getById(id);
    }

    @Override
    public void update(Clazz clazz) {
        //1.补全基础属性
        clazz.setUpdateTime(LocalDateTime.now());
        //2.更新数据
      clazzMapper.update(clazz);
    }

    @Override
    public PageBean page(ClazzQueryParam clazzQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        //2.执行查询
        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);
        //3,解析封装分页结果
        Page<Clazz> p = (Page<Clazz>) clazzList;

        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public List<Clazz> selectAll() {
        return clazzMapper.findAll();
    }


//    @Override
//    public List<Clazz> selectAll(Clazz clazz) {
//        return clazzMapper.queryClazzs(String name,
//                @Param("begin") LocalDate begin,
//                @Param("end") LocalDate end,
//        @Param("page") int page,
//        @Param("pageSize") int pageSize);
//    }

    /*@Override
    public List<Clazz> selectAll(Clazz clazz) {
        return selectAll(clazz);
    }

    @Override
    public PageInfo<Clazz> getByBage(Clazz clazz, Integer page, Integer size) {
        PageHelper.startPage(page,size);

        List<Clazz> clazzList = this.selectAll(clazz);

        return PageHelper.of;
    }*/
}
